package cn.edu.zjgsu.ito.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.edu.zjgsu.ito.model.StudentGrade;
import cn.edu.zjgsu.ito.service.ServiceFactory;
import cn.edu.zjgsu.ito.service.StudentScoreService;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class StudentScoreController extends Controller {

	StudentScoreService studentScoreService=ServiceFactory.createStudentScoreService();
	/**
	 * 学生成绩查询初始化
	 * 黄凯航
	 */
	public void index()
	{
		String passportNo=getSessionAttr("passportNo");
		System.out.println("学生护照号:"+passportNo);
		String current="";
		String error="1";
		List<Record> terms=Db.find("select * from term ");
		List<Record> term=Db.find("select * from term where current=?",1);
		current=term.get(0).get("termValue");
		System.out.println("学生护照号:"+passportNo+"学期：" + current);
		List<Record> scores = Db.find("select * from student_grade,test where student_grade.testNo=test.testNo and testLock='1' and passportNo=? and test.termValue=?",passportNo,current);
		if(scores.size()==0){
			error="0";}
		System.out.println("学生成绩size+" + scores.size());
		for(int i=0;i<scores.size();i++)
		{      
			List<Record> collegeAndCredit=Db.find("select collegeName,credit from course where courseNo=?",scores.get(i).get("courseNo"));
			scores.get(i).set("collegeName", collegeAndCredit.get(0).getStr("collegeName"));
			scores.get(i).set("credit", collegeAndCredit.get(0).getInt("credit"));
		}
		setAttr("studentGrade",scores);
		setAttr("term",terms);
		setAttr("error",error);
		//renderJson();
		render("score.html");      
	}
	/**
	 * 成绩查询
	 * 黄凯航
	 * @throws ParseException 
	 */
	public void search() throws ParseException
	{ 	
		java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String passportNo=getSessionAttr("passportNo");	
		String term=getPara("term");
		List<Record> terms=Db.find("select * from term where invalid=?",0);
		List<Record> scores = Db.find("select * from student_grade,test where student_grade.testNo=test.testNo and testLock='1' and passportNo=? and test.termValue=?",passportNo,term);

		for(int i=0;i<scores.size();i++)
		{
			List<Record> collegeAndCredit=Db.find("select collegeName,credit from course where courseNo=?",scores.get(i).get("courseNo"));
			scores.get(i).set("collegeName", collegeAndCredit.get(0).getStr("collegeName"));
			scores.get(i).set("credit", collegeAndCredit.get(0).getInt("credit"));

//				String []b= new String[100];
//				Date []date=new Date[100];
//				Date dates=new Date();
//				String Dates="";
//				List<Record> score = Db.find("select * from newpassportno where passportNo=?",scores.get(i).getStr("passportNo"));
//				if(score.size()>1){
//					for(int j=0;j<score.size();j++){
//										b[j] = score.get(j).getStr("time");
//										date[j] = formatter.parse(b[j]);
//									}
//									System.out.println(score.size());
//									for(int a=0;a<score.size()-1;a++){
//										 dates = date[a];
//										int c = (date[a+1]).compareTo(dates);
//										System.out.println("-------");
//										System.out.println(c);
//										System.out.println("++++++++++++++++++++++++++++");
//										if(c>0){
//											dates = date[a+1];
//										}
//										System.out.println(dates);
//										System.out.println("================");
//									}
//									System.out.println("qqqqqqqqq");
//									System.out.println(dates);
//									System.out.println("444444444");
//									Dates = formatter.format(dates);
//									System.out.println(Dates);
//									Record newpassportNo = Db.findFirst("select * from newpassportno where time=?",Dates);
//									System.out.println("111111111");
//									System.out.println(newpassportNo.getStr("newpassportNo"));
//									scores.get(i).set("newpassportNo",newpassportNo.getStr("newpassportNo"));
//					}
//				else{
//					System.out.println("[][][][][][][][][][]");
//					Record newpassportNo = Db.findFirst("select * from newpassportno where passportNo=?",scores.get(i).getStr("passportNo"));
//					System.out.println("111111111");
//					System.out.println(newpassportNo.getStr("newpassportNo"));
//					scores.get(i).set("newpassportNo",newpassportNo.getStr("newpassportNo"));
//				}		
			
		}

		setAttr("term",terms);
		setAttr("studentGrade",scores);
		render("score.html");

	}
	/**
	 * 异步成绩查询
	 * 黄凯航
	 * @throws ParseException 
	 */
	public void ajaxsearch() throws ParseException
	{
		java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String passportNo=getSessionAttr("passportNo");
		String term=getPara("term");
		String error="1";
		List<Record> studentGrade = Db.find("select * from student_grade,test where student_grade.testNo=test.testNo and testLock='1' and passportNo=? and test.termValue=?",passportNo,term);
//		List<Record> studentGrade=Db.find("select * from student_grade,test where student_grade.testNo=test.testNo and test.testLock='1' passportNo=? and test.termValue=?  and testLock='1'",term,passportNo);
		if(studentGrade.size()==0)
			error="0";      
        
		for(int i=0;i<studentGrade.size();i++)
		{
			List<Record> collegeAndCredit=Db.find("select collegeName,credit from course where courseNo=?",studentGrade.get(i).get("courseNo"));
            studentGrade.get(i).set("collegeName", collegeAndCredit.get(0).getStr("collegeName"));
			studentGrade.get(i).set("credit", collegeAndCredit.get(0).getInt("credit"));
//			String []b= new String[100];
//			Date []date=new Date[100];
//			Date dates=new Date();
//			String Dates="";
//			List<Record> score = Db.find("select * from newpassportno where passportNo=?",studentGrade.get(i).getStr("passportNo"));
//			if(score.size()>1){
//				for(int j=0;j<score.size();j++){
//									b[j] = score.get(j).getStr("time");
//									date[j] = formatter.parse(b[j]);
//								}
//								System.out.println(score.size());
//								for(int a=0;a<score.size()-1;a++){
//									 dates = date[a];
//									int c = (date[a+1]).compareTo(dates);
//									System.out.println("-------");
//									System.out.println(c);
//									System.out.println("++++++++++++++++++++++++++++");
//									if(c>0){
//										dates = date[a+1];
//									}
//									System.out.println(dates);
//									System.out.println("================");
//								}
//								System.out.println("qqqqqqqqq");
//								System.out.println(dates);
//								System.out.println("444444444");
//								Dates = formatter.format(dates);
//								System.out.println(Dates);
//								Record newpassportNo = Db.findFirst("select * from newpassportno where time=?",Dates);
//								System.out.println("111111111");
//								System.out.println(newpassportNo.getStr("newpassportNo"));
//								studentGrade.get(i).set("newpassportNo",newpassportNo.getStr("newpassportNo"));
//				}
//			else{
//				System.out.println("[][][][][][][][][][]");
//				Record newpassportNo = Db.findFirst("select * from newpassportno where passportNo=?",studentGrade.get(i).getStr("passportNo"));
//				System.out.println("111111111");
//				System.out.println(newpassportNo.getStr("newpassportNo"));
//				studentGrade.get(i).set("newpassportNo",newpassportNo.getStr("newpassportNo"));
//			}		
		}
		setAttr("studentGrade",studentGrade);
		setAttr("error",error);
		renderJson();
	}
}
